package com.stars.empoly.vmodel

import android.app.Application
import android.text.Editable
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.empoly.model.BannerBean
import com.stars.empoly.model.CityList
import com.stars.empoly.model.ServiceList
import com.stars.empoly.utils.net.NetEngine
import com.stars.empoly.view.SimpleTextWatcher
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.net.IResp
import kotlinx.coroutines.launch


/**
 *
 *on 2020/9/23
 */
open class HomeMechVModel(application: Application) : BRViewModel<BRRepository>(application)  {

    var cityList=MutableLiveData<List<CityList>>()
    var cityId:String?=null


    var index=MutableLiveData<Int>()
    var bean=MutableLiveData<ServiceList>()

    var keyword:String?=null

    var oldText = ""
    var textWatcher = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable?) {
            super.afterTextChanged(s)
            var content = s.toString()
            if (!TextUtils.isEmpty(oldText) && content == oldText) {
                return
            }
            keyword = content
            oldText = content
            //刷新
            firstReq()

        }

    }


    fun toTechnician(id:String?){
        viewModelScope.launch {
           var it= NetEngine.service.platformServiceList(id,null,null)
            if(it.ok()){
                bean.value=it.data

            }

        }


    }

    fun toFollow( id:String?,position:Int?){

        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.follow(id) }
            if(it.ok(true)){
                index.value=position
            }
        }

    }


    fun getCityList(){
        viewModelScope.launch{

            var it=repository().lreq { NetEngine.service.cityList() }

            if(it.ok()){
                cityList.value=it.data
            }
        }

    }


    override fun apiFunc(): suspend () -> IResp<*> {

        return { NetEngine.service.technicianList(keyword,cityId,null,MMKV.defaultMMKV().getString("lat",null), MMKV.defaultMMKV().getString("lng",null),10,repository().page) }


    }


}
